# encoding=utf-8

import pygame
import os
import pickle
import time


class MusicPlay:
	currentSong = ""
	voice = []
	def __init__(self):
		pygame.mixer.init()
		self.context = "./audio/"
		if not os.path.exists("VoiceTable"):
			f = open("VoiceTable", "wb")
			for i in range(15):
				self.voice.append(["",""])
			pickle.dump(self.voice, f)
			f.close()
		else:
			f = open("VoiceTable", "rb")
			self.voice = pickle.load(f)
			f.close()

	def SelectFile(self, str):
		for item in self.voice:
			if item[0]==str:
				self.currentSong = self.context + item[1]
				return True
		self.currentSong = ""
		return False


	def play(self, loop=0, start=0):
		if self.currentSong != "":
			pygame.mixer.music.load(self.currentSong)
			pygame.mixer.music.play(loop, start)
			time.sleep(3)

	def configure(self):
		print("语音名称 \t ", "文件名")
		for i in range(len(self.voice)):
			self.voice[i][0] = input(" ")
			self.voice[i][1] = input("\n")
		f = open("VoiceTable", "wb")
		print(self.voice)
		pickle.dump(self.voice, f)
		f.close()


if __name__ == "__main__":
	play = MusicPlay()
	name = "6翻了"
	play.SelectFile(name)
	play.play()
	#play.configure()
	#print(play.voice)
